package com.glory.brepda.activity.technicalservice;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.glory.brepda.R;
import com.glory.brepda.adapter.RepairCheckListAdapter;
import com.glory.brepda.adapter.RepairFromListAdapter;
import com.glory.brepda.base.BaseActivity;
import com.glory.brepda.base.Constant;
import com.glory.brepda.entity.RepairFromListEntity;
import com.glory.brepda.entity.response.BaseResponseBean;
import com.glory.brepda.utils.NetUtils;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.lzy.okgo.model.Response;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_DialogEntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_EntityCallBack;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class TechnicalCheckActivity extends BaseActivity {

    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvBaseSubmit)
    TextView tvBaseSubmit;
    @BindView(R.id.etEqpWork)
    FreeUI_ClearEditText etEqpWork;
    @BindView(R.id.etEqpPos)
    FreeUI_ClearEditText etEqpPos;
    @BindView(R.id.etUser)
    FreeUI_ClearEditText etUser;
    @BindView(R.id.tvUnFinish)
    TextView tvUnFinish;
    @BindView(R.id.tvFinish)
    TextView tvFinish;
    @BindView(R.id.tvTechnicalDemand)
    TextView tvTechnicalDemand;
    @BindView(R.id.tvFaultRepair)
    TextView tvFaultRepair;
    @BindView(R.id.tvRepairID)
    TextView tvRepairID;
    @BindView(R.id.tvRepairType)
    TextView tvRepairType;
    @BindView(R.id.tvPositionAndJobNo)
    TextView tvPositionAndJobNo;
    @BindView(R.id.rvTechnicalList)
    RecyclerView rvTechnicalList;
    @BindView(R.id.llMain)
    LinearLayout llMain;

    private List<RepairFromListEntity.RepairData> mFinList;
    private List<RepairFromListEntity.RepairData> mUnFinList;
    private RepairCheckListAdapter mRepairAdapter;
    private boolean status = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_technical_demand;
    }

    @Override
    protected void initView() {
        setMainLayout(llMain);
        tvTitle.setText("技术需求验收");
        tvBaseSubmit.setVisibility(View.GONE);
        tvFaultRepair.setVisibility(View.GONE);
        tvTechnicalDemand.setVisibility(View.GONE);
        initRecyclerView(rvTechnicalList,this);
        initEdit(etEqpWork, s -> initData());
        initEdit(etEqpPos, s -> initData());
        initEdit2(etUser, s -> initData());
        mFinList = new ArrayList<>();
        mUnFinList = new ArrayList<>();
        mRepairAdapter = new RepairCheckListAdapter(null,false);
        mRepairAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mRepairAdapter.isFirstOnly(true);
        rvTechnicalList.setAdapter(mRepairAdapter);
    }

    @Override
    protected void initData() {
        FreeUI_EntityCallBack<BaseResponseBean<RepairFromListEntity>> callback = new FreeUI_DialogEntityCallBack<BaseResponseBean<RepairFromListEntity>>
                (new TypeToken<BaseResponseBean<RepairFromListEntity>>() {
                }.getType(), getSupportFragmentManager(), this) {

            @Override
            public void onSuccess(Response<BaseResponseBean<RepairFromListEntity>> response) {
                super.onSuccess(response);
                if (response.body().isSuccess(getApplicationContext(), true, getSupportFragmentManager())) {
                    RepairFromListEntity body = response.body().getResponse().getBody();
                    mFinList.clear();
                    mUnFinList.clear();
                    for (RepairFromListEntity.RepairData entity : body.getREPAIRFORMLIST()){
                        if (entity.getREPAIRFORM().getSTATUS().equals("FORM") && !entity.getREPAIRSHEETDATA().getREPAIRTYPE().contains("change")){
                            mFinList.add(entity);
                        }else {
                            mUnFinList.add(entity);
                        }
                    }

                    listSort(mFinList);
                    listSort(mUnFinList);
                    mRepairAdapter.setNewData(mFinList);
                    setViewStates(true);

                }
            }

            @Override
            public void onError(Response<BaseResponseBean<RepairFromListEntity>> response) {
                super.onError(response);
                loadError(response.getException(),"REPAIRFORMCHECK");
            }
        };
        JsonObject map = new JsonObject();
        map.addProperty("ACTIONTYPE","COMPLETE");
        map.addProperty("EQUIPMENTID",getText(etEqpWork));
        map.addProperty("EQUIPMENTGROUP","");
        map.addProperty("REPAIRFORMRRN","");
        map.addProperty("EQPPOSITION",getText(etEqpPos));
        map.addProperty("USERNAME",getText(etUser).isEmpty() ? Constant.CURRENT_USER : getText(etUser));
        NetUtils.requestPostNet("IF.REPAIRFORMCHECK","REPAIRFORMQUERY", TechnicalCheckActivity.this,map,callback);
    }

    private void listSort(List<RepairFromListEntity.RepairData> list) {
        Collections.sort(list, (repairData, t1) -> {
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
            try {
                Date dt1 = format.parse(repairData.getREPAIRSHEETDATA().getTROUBLETIME());
                Date dt2 = format.parse(t1.getREPAIRSHEETDATA().getTROUBLETIME());
                if (dt1.getTime() < dt2.getTime()) {
                    return 1;
                } else if (dt1.getTime() > dt2.getTime()) {
                    return -1;
                } else {
                    return 0;

                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return 0;
        });
    }

    @OnClick({R.id.tvUnFinish, R.id.tvFinish})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tvUnFinish:
                setViewStates(true);
                break;
            case R.id.tvFinish:
                setViewStates(false);
                break;
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        initData();
    }

    private void setViewStates(boolean states) {
        tvUnFinish.setBackgroundColor(states ? getResources().getColor(R.color.free_ui_blue_sky_theme_color) : getResources().getColor(R.color.white));
        tvUnFinish.setTextColor(states ? getResources().getColor(R.color.white) : getResources().getColor(R.color.black));
        tvFinish.setBackgroundColor(states ? getResources().getColor(R.color.white) : getResources().getColor(R.color.free_ui_blue_sky_theme_color) );
        tvFinish.setTextColor(states ? getResources().getColor(R.color.black) : getResources().getColor(R.color.white));
        this.status = states;
        mRepairAdapter.setNewData(status ? mFinList : mUnFinList);
    }
}
